package com.tear.language.node;

import java.util.Map;

import net.sf.json.JSONObject;
import ognl.Ognl;

import org.apache.commons.lang.StringUtils;

import com.tear.language.utils.Attrs;
import com.tear.language.utils.TypeUtil;

/**

	选择操作--类似与switch-case中的case

*/
public class WhenNode extends BaseNode {

	@Override
	public boolean parse(Map<String, Object> currParams,Map<String, Object> globalParams, JSONObject obj) throws Exception {
		boolean whenFlag = false;
		if(currParams.containsKey(Attrs.WHEN_FLAG)) {
			whenFlag = (Boolean)currParams.get(Attrs.WHEN_FLAG);
		}
		String testStr = obj.optString(Attrs.CONDITION);
		if(StringUtils.isNotEmpty(testStr)) {
			testStr = TypeUtil.replaceArray(testStr);
		}
		boolean test = false;
		if(StringUtils.isNotEmpty(testStr) && !whenFlag) {
			Map<String, Object> allParams = getAllParams(currParams,globalParams);
			test = (Boolean)Ognl.getValue(testStr,allParams);
			if(test) currParams.put(Attrs.WHEN_FLAG, true);
		}
		return test;
		
	}

}
